Splitting a string by full word

I have been looking on this site for a method that matched my scenario, but couldn't find one.  I have a string that says something like "Rqmt Chg Request 123,Rqmt Chg Request 127,Rqmt Chg Request 231" and I need to extract the numeric values.  I've seen a LOT of examples where a string is split by a single character like a comma or semicolon, but I really want to split the string on "Rqmt Chg Request " to give me the number.  I know that in JavaScript it would be str.split("Rqmt Chg Request ") which would give me an array containing the numbers, but, as I know, DXL doesn't have a similar split function.  These strings could have any number of "Rqmt Chg Request" substrings included, so how do I get each of the numeric values so I can use them in another function?

 

Chris


chrscote - Mon Apr 10 14:25:09 EDT 2017

Re: Splitting a string by full word
Jstein - Mon Apr 10 14:36:39 EDT 2017

Hi Chris.

I had this same trouble when I need to get the absolute number of an object.
Then i  use this:

int getArraySize(Array a){
    int size = 0;
    while( !null(get(a, size, 0) ) )
        size++;
    return size;
}


void addArrayString(Array a, string str){
    int array_index = getArraySize(a);

    put(a, str, array_index, 0);
}


Array split(string str, string splitter){
        Array tokens = create(1, 1);
        Buffer buf = create;
        int str_index;

        buf = "";

        for(str_index = 0; str_index < length(str); str_index++){
            if( str[str_index:str_index] == splitter ){
                addArrayString(tokens, stringOf(buf));
                buf = "";
            }else{
                buf += str[str_index:str_index];
            }
        }
        addArrayString(tokens, stringOf(buf));
        delete buf;
       return tokens
}


string getArrayString(Array a, int index){
   return (string get(a, index, 0));
}


 Array splitedAbsNmbr = split("Rqmt Chg Request 123"," ");
int absNmbr =  intOf(getArrayString(splitedAbsNmbr, 3));

print absNmbr

 

 



Maybe you will need to adapt the code.

Regards.

Re: Splitting a string by full word
chrscote - Mon Apr 10 15:13:54 EDT 2017

Jstein - Mon Apr 10 14:36:39 EDT 2017

Hi Chris.

I had this same trouble when I need to get the absolute number of an object.
Then i  use this:

int getArraySize(Array a){
    int size = 0;
    while( !null(get(a, size, 0) ) )
        size++;
    return size;
}


void addArrayString(Array a, string str){
    int array_index = getArraySize(a);

    put(a, str, array_index, 0);
}


Array split(string str, string splitter){
        Array tokens = create(1, 1);
        Buffer buf = create;
        int str_index;

        buf = "";

        for(str_index = 0; str_index < length(str); str_index++){
            if( str[str_index:str_index] == splitter ){
                addArrayString(tokens, stringOf(buf));
                buf = "";
            }else{
                buf += str[str_index:str_index];
            }
        }
        addArrayString(tokens, stringOf(buf));
        delete buf;
       return tokens
}


string getArrayString(Array a, int index){
   return (string get(a, index, 0));
}


 Array splitedAbsNmbr = split("Rqmt Chg Request 123"," ");
int absNmbr =  intOf(getArrayString(splitedAbsNmbr, 3));

print absNmbr

 

 



Maybe you will need to adapt the code.

Regards.

I don't quite understand how the split function is working.  I can see that it is looping thru the string str and looking for the space in your case.  If the character is not a space, you add it to the buffer.  Then when it eventually finds the splitter (space in this case), it adds that string to the tokens array and clears out the buffer.  But how is it giving the number and not the other parts of the string?  Is it from the getArrayString?

  Also, is there no other way to get the size of an array in DXL?  It seems like a little bit of a waste of CPU usage to have to loop through the array each time...

Chris

Re: Splitting a string by full word
Mathias Mamsch - Tue Apr 11 04:39:14 EDT 2017

The easiest thing (and probably most performant thing) if you want to do string splits, etc. in DXL is to use a regular expression, because they support very easy looping. I did not test this thouroughly, so this is just for getting you started: 

Buffer gBufSplit = create(); 

// Note: this function returns a skip! Do not forget to free it after usage
Skip split(string s, Regexp re) {
    int pos = 0; 
    gBufSplit = s
    
    Skip skMatches = create()
    while (search(re, gBufSplit, pos)) {
        // we found a match from pos+start 0 - to pos+(end 0), therefore we need to 
        string sMatch = ""; 
        if (start 0 > 0) sMatch = gBufSplit[pos:pos+(start 0)-1]

        put(skMatches, pos, sMatch )
        pos += (end 0) + 1
    }

    if (pos < length s) {
       // put the rest of the string in the buffer is there is something
       string sMatch = gBufSplit[pos:]; 
         put(skMatches, pos, sMatch)
    }
    return skMatches 
}

string s = "World!World!You cannot have moreWorld! World!";
string s2
for s2 in split(s, regexp2 "World!") do print s2 ","
print "\n"

s = "xxHello World!World!You cannot have moreWorld! World!yy";
for s2 in split(s, regexp2 "World!") do print s2 ","
print "\n"

Maybe this helps, regards, Mathias

Re: Splitting a string by full word
chrscote - Tue Apr 11 08:53:17 EDT 2017

Mathias Mamsch - Tue Apr 11 04:39:14 EDT 2017

The easiest thing (and probably most performant thing) if you want to do string splits, etc. in DXL is to use a regular expression, because they support very easy looping. I did not test this thouroughly, so this is just for getting you started: 

Buffer gBufSplit = create(); 

// Note: this function returns a skip! Do not forget to free it after usage
Skip split(string s, Regexp re) {
    int pos = 0; 
    gBufSplit = s
    
    Skip skMatches = create()
    while (search(re, gBufSplit, pos)) {
        // we found a match from pos+start 0 - to pos+(end 0), therefore we need to 
        string sMatch = ""; 
        if (start 0 > 0) sMatch = gBufSplit[pos:pos+(start 0)-1]

        put(skMatches, pos, sMatch )
        pos += (end 0) + 1
    }

    if (pos < length s) {
       // put the rest of the string in the buffer is there is something
       string sMatch = gBufSplit[pos:]; 
         put(skMatches, pos, sMatch)
    }
    return skMatches 
}

string s = "World!World!You cannot have moreWorld! World!";
string s2
for s2 in split(s, regexp2 "World!") do print s2 ","
print "\n"

s = "xxHello World!World!You cannot have moreWorld! World!yy";
for s2 in split(s, regexp2 "World!") do print s2 ","
print "\n"

Maybe this helps, regards, Mathias

Thank you Mathias.  This seems to work, though I noticed that the skipList included empty strings for each "World!" string.  I'm also not sure what the start and end functions are doing, but I'll look those up in the reference manual.

 

Chris

Re: Splitting a string by full word
Mathias Mamsch - Tue Apr 11 09:59:52 EDT 2017

chrscote - Tue Apr 11 08:53:17 EDT 2017

Thank you Mathias.  This seems to work, though I noticed that the skipList included empty strings for each "World!" string.  I'm also not sure what the start and end functions are doing, but I'll look those up in the reference manual.

 

Chris

well, when you split   "x/x/x"  with "/" as a separator you expect:  "x","x","x"

when you remove the x now and split  "//" you would expect three empty strings "","","". 

At least this is what split does in other languages. Regards, Mathias

Re: Splitting a string by full word
JoeMarfice - Fri Nov 16 14:20:09 EST 2018

Jstein - Mon Apr 10 14:36:39 EDT 2017

Hi Chris.

I had this same trouble when I need to get the absolute number of an object.
Then i  use this:

int getArraySize(Array a){
    int size = 0;
    while( !null(get(a, size, 0) ) )
        size++;
    return size;
}


void addArrayString(Array a, string str){
    int array_index = getArraySize(a);

    put(a, str, array_index, 0);
}


Array split(string str, string splitter){
        Array tokens = create(1, 1);
        Buffer buf = create;
        int str_index;

        buf = "";

        for(str_index = 0; str_index < length(str); str_index++){
            if( str[str_index:str_index] == splitter ){
                addArrayString(tokens, stringOf(buf));
                buf = "";
            }else{
                buf += str[str_index:str_index];
            }
        }
        addArrayString(tokens, stringOf(buf));
        delete buf;
       return tokens
}


string getArrayString(Array a, int index){
   return (string get(a, index, 0));
}


 Array splitedAbsNmbr = split("Rqmt Chg Request 123"," ");
int absNmbr =  intOf(getArrayString(splitedAbsNmbr, 3));

print absNmbr

 

 



Maybe you will need to adapt the code.

Regards.

DXL has an operator sizeof, so instead of creating the function getArraySize(a), you could simply have written

int array_index = sizeof a

HTH.